
//
//  HCHomeAnswerView.m
//  HappyChildren
//
//  Created by WillyZhao on 2017/1/17.
//  Copyright © 2017年 WillyZhao. All rights reserved.
//

#import "HCHomeAnswerView.h"

#import "HCAnswerTableViewCell.h"

#import "LTInputAccessoryView.h"

#import "HCAnswerHomeModel.h"


@interface HCHomeAnswerView () <UITableViewDelegate,HCKeyBoardViewDelegate,UITableViewDataSource> {
    UITableView *contentTableView_;
    LTInputAccessoryView *keyBoardView_;
    HCAnswerHomeModel *cen_model;
}

@end

@implementation HCHomeAnswerView

-(instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self initializeSubViews:frame];
    }
    return self;
}

-(void)initializeSubViews:(CGRect)frame {
    
    
    contentTableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, 10, frame.size.width, frame.size.height-10)];
    contentTableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
    contentTableView_.rowHeight = (frame.size.height-10)/4.0;
    contentTableView_.backgroundColor = YBKRGBCOLOR(242, 242, 242, 1);
    contentTableView_.delegate = self;
    contentTableView_.dataSource = self;
    contentTableView_.showsVerticalScrollIndicator = NO;
    [self addSubview:contentTableView_];
    
    [contentTableView_ registerNib:[UINib nibWithNibName:@"HCAnswerTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"answer"];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (cen_model) {
        return cen_model.ask.count;
    }else{
        return 0;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HCAnswerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"answer" forIndexPath:indexPath];
    
    if (cen_model) {
        HCAnswerHomeData *data = (HCAnswerHomeData *)cen_model.ask[indexPath.row];
        cell.model = data;
    }
    
    [cell.answerBtn addTarget:self action:@selector(answerBtn:event:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

-(void)answerBtn:(UIButton *)btn event:(id)event{
    
    //获取点击的具体位置
    NSSet *touches = [event allTouches];
    UITouch *touch = [touches anyObject];
    
    CGPoint currentTouchPosition = [touch locationInView:contentTableView_];
    NSIndexPath *indexPath = [contentTableView_ indexPathForRowAtPoint:currentTouchPosition];
    
    __block __weak HCHomeAnswerView *weakself = self;
    
    keyBoardView_ = [LTInputAccessoryView new];
    keyBoardView_.delegate = self;
    [keyBoardView_ showBlock:^(NSString *contentStr) {
        //做网络请求
        if ([weakself.delegate respondsToSelector:@selector(answerBtnClicked:andInpuString:)] && weakself.delegate) {
            [weakself.delegate answerBtnClicked:indexPath andInpuString:contentStr];
        }
    }];
}

-(void)showAlertStr:(NSString*)str{
    UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"LTInputAccessoryView" message:[NSString stringWithFormat:@"输入的文字是:%@",str] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"确认", nil];
    [alert show];
}

-(void)setModel:(id)model {
    cen_model = (HCAnswerHomeModel *)model;
    [contentTableView_ reloadData];
}

//#pragma mark HCKeyBoardViewDelegate
//-(void)hc_keyBoardWillDismiss:(CGFloat)height {
//    NSLog(@"键盘即将隐藏");
//    [UIView animateWithDuration:0.25 animations:^{
//        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y+height, self.frame.size.width, self.frame.size.height);
//    }];
//}
//
//-(void)hc_keyBoardWillShow:(CGFloat)height time:(CGFloat)time{
//    NSLog(@"键盘即将显示");
//    
//    [UIView animateWithDuration:time animations:^{
//        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y-height, self.frame.size.width, self.frame.size.height);
//    }];
//}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {*cell =
    // Drawing code
}
*/

@end
